package com.pearlsoft.webapp.sysmgr.role;

import org.apache.commons.lang.StringUtils;

import com.pearlsoft.webapp.core.annotation.DataSetHandlerComponent;
import com.pearlsoft.webapp.core.dataset.DataSet;
import com.pearlsoft.webapp.core.dataset.Record;
import com.pearlsoft.webapp.core.dataset.Result;
import com.pearlsoft.webapp.core.web.basehandler.AbsDataSetHandler;
import com.pearlsoft.webapp.sysmgr.role.service.RoleService;

/**
 * 
 * @author YEHH
 *
 */
@DataSetHandlerComponent(name = "SysRoleDataSetHandler")
public class SysRoleDataSetHandler extends AbsDataSetHandler {

	public void handler(DataSet ds, Result result) throws Exception {

	}

	public void save(final DataSet ds, Result result) throws Exception {
		Record record = ds.getCurrentRecord();
		RoleService roleService = this.getBean("roleService", RoleService.class);
		roleService.save(record);
		result.getRecords().add(record);
	}
	
	
	public void delete(final DataSet ds, Result result) throws Exception {
		RoleService roleService = this.getBean("roleService", RoleService.class);
		String objId = (String) ds.getConditions().get("objId");
		roleService.deleteById(objId);
	}
	
	public void deleteAll(final DataSet ds, Result result) throws Exception {
		RoleService roleService = this.getBean("roleService", RoleService.class);
		String objIds = (String) ds.getConditions().get("objIds");
		String[] ids = objIds.split(",");
		for(String id:ids){
			if( !StringUtils.isBlank(id)){
				roleService.deleteById(id);
			}
		}
	}
	
	public void saveRoleMenu(final DataSet ds, Result result) throws Exception {
		String roleId = (String)ds.getConditions().get("ROLE_ID");
		String menuIds= (String) ds.getConditions().get("MENU_IDS");
		RoleService roleService = this.getBean("roleService", RoleService.class);
		roleService.saveRoleMenu(roleId, menuIds);
	}
	
}
